<?php defined('BASEPATH') OR exit('No direct script access allowed');



class Attachment extends MY_Controller {
	

	/**
	 * 构造函数
	 * @公共 访问
	 */
	public function __construct(){
		parent::__construct();
		$this->load->helper('download');
		$this->load->helper('string');
	}

	//判断文件是否存在
	function _att_exist_post(){
		$data = $this->input->post(NULL, TRUE);
		$date_rand = explode('_',$data['att_id']);		//分解为数组
		$status = false;
		if(count($date_rand) == 2){
			$data['att_name'] = urldecode($data['att_name']);
			$data['att_name'] = iconv( "UTF-8", "GBK//IGNORE" , $data['att_name']);
	
			$file_dir = FCPATH.'attach\\'.$data['m'].'\\'.$date_rand[0].'\\'.$date_rand[1].'.'.$data['att_name'];
			$status = file_exists($file_dir);
		}
		echo json_encode(array("status"=>$status)); 
	}
	
	//下载、打开附件
	function _att_down_post(){
		$data = $this->input->post(NULL, TRUE);
		$date_rand = explode('_',$data['att_id']);		//分解为数组
		$data['att_name'] = urldecode($data['att_name']);
		$att_name = iconv( "UTF-8", "GBK//IGNORE" , $data['att_name']);
		$file_dir = FCPATH.'attach\\'.$data['m'].'\\'.$date_rand[0].'\\'.$date_rand[1].'.'.$att_name;

		$data = file_get_contents($file_dir); // 读文件内容
		force_download($att_name, $data);
	}
	
	//载入OFFICE文件阅读、编辑视图
	function _att_read_post(){
		$data = $this->input->post(NULL, TRUE);
		//$this->att->att_read($data);
		$data['date_rand'] = explode('_',$data['att_id']);
		$data['att_name'] = urlencode($data['att_name']);
		$this->load->view('oc/index',$data);
	}
	//获取OFFICE文件编辑后的文件，并替换
	function _save_copy_doc_post(){
		$urlstr = $this->input->post('url', TRUE);
		if ($_FILES["FileData"]["error"] > 0){
			echo "Return Code: " . $_FILES["FileData"]["error"] . "<br />";
		}else{
			
			//echo "Upload: " . $_FILES["FileData"]["name"] . "<br />";
			//echo "Type: " . $_FILES["FileData"]["type"] . "<br />";
			//echo "Size: " . ($_FILES["FileData"]["size"] / 1024) . " Kb<br />";
			//echo "Temp file: " . $_FILES["FileData"]["tmp_name"] . "<br />";
			
			if(move_uploaded_file($_FILES["FileData"]["tmp_name"],FCPATH.'attach/'.$urlstr['url'].$_FILES["FileData"]["name"])){
				$status = true;
			}else{
				$status = false;	
			}
			echo json_encode(array("status"=>$status)); 
		}
	}
	
	//上传文件
	function _upload_post(){
		$data = $this->input->post(NULL,TRUE);
		$datedir = date('ym');
		$upload_path = FCPATH.'attach/'.$data['attdir'].'/'.$datedir;
		if(!is_dir($upload_path)) mkdir($upload_path, 0755); 		
		
		$verifyToken = md5('unique_salt' . $data['timestamp']);
		if (!empty($_FILES) && $data['token'] == $verifyToken) {	
			$config['upload_path']	 =	$upload_path;
			//$config['allowed_types'] =	reduce_multiples(implode('|',$this->attachment_type), "|", TRUE);//数组转字符串以竖线分割并去首尾和重复的竖线
			$config['allowed_types'] =	'*';
			$config['rand_prefix']	 =	random_string('nozero',9);
			$this->load->library('upload',$config);
			if($this->upload->do_upload('Filedata')){
				$arr = $this->upload->data();				
				$data = array("flag"=>true,"str"=>$datedir.'_'.$arr['rand_prefix'],"file_name"=>$arr['orig_name']);
			}else{
				log_message('error',$_FILES['Filedata']['name'].$_FILES['Filedata']['type']);
				$data = array("flag"=>false,"str"=>$this->upload->display_errors());
			}
			echo json_encode($data); 
		}
	}
	
	//删除文件
	function _upload_del_post(){
		$data = $this->input->post(NULL,TRUE);
		$data_arr = json_decode($data['data'],true);
		
		$date_rand = explode('_',$data_arr['daterand']);//把日期、随机数组合进行拆分
		$file_name = iconv("UTF-8","GBK//IGNORE",$data_arr['name']);//转换为GBK，防止中文文件名出现乱码
		$file = FCPATH.'attach/'.$data['m'].'/'.$date_rand[0].'/'.$date_rand[1].'.'.$file_name;//拼接文件地址
		
		if(file_exists($file)){
			$status = !unlink($file)?false:true;//三目判断删除状态
		}else{
			$status = false;
		}
		$status = array("status"=>$status);
		echo json_encode($status);//返回JSON格式数据
	}

}